﻿using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UIFramework;

namespace Common.Utils
{
    public class EventTriggerListener : EventTrigger
    {
        public delegate void InputHandle(GameObject go, BaseEventData eventData);
        private InputHandle onClick;
        private InputHandle onDoubleClick;
        private InputHandle onDown;
        private InputHandle onUp;
        private InputHandle onEnter;
        private InputHandle onExit;
        private InputHandle onSelect;
        private InputHandle onUpdateSelect;
        private InputHandle onDeSelect;
        private InputHandle onDrag;
        private InputHandle onDragEnd;
        private InputHandle onDrop;
        private InputHandle onScroll;
        private InputHandle onMove;


        static public EventTriggerListener Get(GameObject _go)
        {
            EventTriggerListener listener = _go.GetComponent<EventTriggerListener>();
            if (listener == null) listener = _go.AddComponent<EventTriggerListener>();
            return listener;
        }


        #region Set EventHandle
        /// <summary>
        /// Sets the event handle.
        /// </summary>
        /// <param name="_uiEventType">Event type.</param>
        /// <param name="_handle">Handle.</param>
        public void SetEventHandle(ENUM_UIEVENT_TYPE _uiEventType, InputHandle _handle)
        {
            switch(_uiEventType)
            {
                case ENUM_UIEVENT_TYPE.ONCLICK:
                    onClick = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONDOUBLECLICK:
                    onDoubleClick = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONDOWN:
                    onDown = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONUP:
                    onUp = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONENTER:
                    onEnter = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONEXIT:
                    onExit = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONSELECT:
                    onSelect = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONUPDATESELECT:
                    onUpdateSelect = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONDESELECT:
                    onDeSelect = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONDRAG:
                    onDrag = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONDRAGEND:
                    onDragEnd = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONDROP:
                    onDrop = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONSCROLL:
                    onScroll = _handle;
                    break;
                case ENUM_UIEVENT_TYPE.ONMOVE:
                    onMove = _handle;
                    break;
                default:
                    break;
            }
        }
        #endregion


        #region IPointerClickHandler implementation
        public override void OnPointerClick(PointerEventData eventData)
        {
            if (null != onClick)
                onClick(gameObject, eventData);
        }
        #endregion


        #region IPointerDownHandler implementation
        public override void OnPointerDown(PointerEventData eventData)
        {
            if (null != onDown)
                onDown(gameObject, eventData);
        }
        #endregion


        #region IPointerUpHandler implementation
        public override void OnPointerUp(PointerEventData eventData)
        {
            if (null != onUp)
                onUp(gameObject, eventData);
        }
        #endregion


        #region IPointerEnterHandler implementation
        public override void OnPointerEnter(PointerEventData eventData)
        {
            if (null != onEnter)
                onEnter(gameObject, eventData);
        }
        #endregion


        #region IPointerExitHandler implementation
        public override void OnPointerExit(PointerEventData eventData)
        {
            if (null != onExit)
                onExit(gameObject, eventData);
        }
        #endregion


        #region ISelectHandler implementation
        public override void OnSelect(BaseEventData eventData)
        {
            if (null != onSelect)
                onSelect(gameObject, eventData);
        }
        #endregion


        #region IUpdateSelectedHandler implementation
        public override void OnUpdateSelected(BaseEventData eventData)
        {
            if (null != onUpdateSelect)
                onUpdateSelect(gameObject, eventData);
        }
        #endregion


        #region IDeselectHandler implementation
        public override void OnDeselect(BaseEventData eventData)
        {
            if (null != onDeSelect)
                onDeSelect(gameObject, eventData);
        }
        #endregion


        #region IDragHandler implementation
        public override void OnDrag(PointerEventData eventData)
        {
            if (null != onDrag)
                onDrag(gameObject, eventData);
        }
        #endregion


        #region IEndDragHandler implementation
        public override void OnEndDrag(PointerEventData eventData)
        {
            if (null != onDragEnd)
                onDragEnd(gameObject, eventData);
        }
        #endregion


        #region IDropHandler implementation
        public override void OnDrop(PointerEventData eventData)
        {
            if (null != onDrop)
                onDrop(gameObject, eventData);
        }
        #endregion



        #region IScrollHandler implementation
        public override void OnScroll(PointerEventData eventData)
        {
            if (null != onScroll)
                onScroll(gameObject, eventData);
        }
        #endregion


        #region IMoveHandler implementation
        public override void OnMove(AxisEventData eventData)
        {
            if (null != onMove)
                onMove(gameObject, eventData);
        }
        #endregion
    }
}

